package com.gitee.mazhenggg.demo.auth.service;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.gitee.mazhenggg.demo.auth.entity.Role;
import com.gitee.mazhenggg.demo.auth.entity.SysUser;
import com.gitee.mazhenggg.demo.auth.security.SecurityUserDetails;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserService userService;




    private Collection<? extends GrantedAuthority> obtainGrantedAuthorities(Set<Role> roles) {
        return roles.stream().map(role -> new SimpleGrantedAuthority(role.getCode())).collect(Collectors.toSet());
    }
    @Override
    @SneakyThrows
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        if (StringUtils.isBlank(username)) {
            throw new UsernameNotFoundException("用户名不能为空");
        }

        SysUser user = userService.getUserByName(username);
        if (null != user) {
            log.debug("load user by uniqueId :{}, {}", username, user);
            log.debug("user:{},roles:{}", user.getUsername(), user.getRoles());
            String currentRole = "";
//            List<MenuTree> menuTrees = null;
//            if (null != user.getRoles()) {
//                Iterator<Role> roleIterable = user.getRoles().iterator();
//                if (roleIterable.hasNext()) {
//                    // 默认选中第一个角色作为默认角色
//                    currentRole = roleIterable.next().getCode();
//                    menuTrees = userService.getMenuTreeByRoleCode(currentRole);
//                }
//            }
//            Set<String> dbAuthsSet = new HashSet<>();
//            if (ArrayUtil.isNotEmpty(user.getRoles())) {
//                // 获取角色
//                Arrays.stream(user.getRoles()).forEach(role -> dbAuthsSet.add(SecurityConstants.ROLE + role));
//                // 获取资源
//                dbAuthsSet.addAll(Arrays.asList(user.getPermissions()));
//
//            }
//            Collection<? extends GrantedAuthority> authorities = AuthorityUtils
//                    .createAuthorityList(dbAuthsSet.toArray(new String[0]));
            return new SecurityUserDetails(user,
                    this.obtainGrantedAuthorities(user.getRoles()));
        } else {
            throw new UsernameNotFoundException(username + " 未找到");
        }
    }
}
